package com.woniuxy.springboot01.service.impl;

import com.woniuxy.springboot01.mapper.AccountMapper;
import com.woniuxy.springboot01.service.AccountService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author author
 * @Create 2023-09-08 17:21
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    AccountMapper accountMapper;

//    @Autowired
//    AccountService accountService;

    @Transactional()
    @Override
    public void changeAccount(String from, String to, int balance) {
            accountMapper.updateAccount(from, balance);
            if(true){
                //受查异常
                throw new RuntimeException("转账异常");
            }
            accountMapper.updateAccount(to, balance*-1);
    }

    @Override
    public void otherMethod() {
//        System.out.println("this="+this.getClass());
        AccountService accountServiceProxy = (AccountService) AopContext.currentProxy();
        System.out.println("accountServiceProxy="+accountServiceProxy.getClass());
        accountServiceProxy.changeAccount("zs", "ls", 100);
        //事务失效：用的是this目标对象
//        changeAccount("zs", "ls", 100);
        //在本类中注入自己：accountService=class com.woniuxy.springboot01.service.impl.AccountServiceImpl$$EnhancerBySpringCGLIB$$369ab481
        //代理对象才会织入事务管理的功能
//        accountService.changeAccount("zs", "ls", 100);
    }


}
